package com.sz.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sz.mes.entity.Technique;
import com.sz.mes.mapper.TechniqueMapper;
import com.sz.mes.vo.GenerateTechniqueVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.sz.mes.entity.ClockIn;
import com.sz.mes.dto.ClockInDTO;
import com.sz.mes.vo.ClockInListVO;
import com.sz.mes.vo.ClockInDetailVO;
import com.sz.mes.service.ClockInService;
import com.sz.mes.controller.BaseCustomCrudRestController;
import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 上下机记录 相关Controller
 *
 * @author hem
 * @version 1.0
 * @date 2023-06-19
 * Copyright © szhs
 */
@RestController
@RequestMapping("/clockIn")
@BindPermission(name = "上下机记录")
@Slf4j
public class ClockInController extends BaseCustomCrudRestController<ClockIn> {

    @Autowired
    private ClockInService clockInService;

    @Autowired
    private TechniqueMapper techniqueMapper;

    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    // @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(ClockInDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, ClockInListVO.class);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    // @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
        return super.getViewObject(id, ClockInDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    // @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody ClockIn entity) throws Exception {
        QueryWrapper<Technique> tqw = new QueryWrapper<>();
        tqw.eq("task_num", entity.getTaskNum());
        tqw.eq("tech_name", entity.getTechName());
        List<Technique> techniqueList = techniqueMapper.selectList(tqw);
        tqw.eq("work_equipment", entity.getEquipmentNumber());
        Technique technique = techniqueMapper.selectOne(tqw);
        // if (technique != null) {
        // technique.setIsUsed(0);
        // }
        // technique.setOutput(technique.getOutput() + entity.getOutput());
        // int i = techniqueMapper.updateById(technique);
        if (techniqueList.size() != 1) {
            for (Technique technique1 : techniqueList) {
                technique1.setOutput(technique1.getOutput() + entity.getGoodQuantity() + entity.getBadQuantity());
                techniqueMapper.updateById(technique1);
            }
        } else {
            technique.setOutput(entity.getGoodQuantity() + entity.getBadQuantity());
            techniqueMapper.updateById(technique);
        }
        return super.createEntity(entity);
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    // @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody ClockIn entity) throws Exception {
        return super.updateEntity(id, entity);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    // @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
        return super.deleteEntity(id);
    }

    /**
     * 生成工序任务
     *
     * @param
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    // @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/generateTechnique")
    public JsonResult generateTechnique(@Valid @RequestBody GenerateTechniqueVO generateTechniqueVO) throws Exception {
        return clockInService.generateTechnique(generateTechniqueVO);
    }

    /**
     * 上机
     *
     * @param
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    // @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/upplane")
    public JsonResult upplane(@Valid @RequestBody ClockIn clockIn) throws Exception {
        return clockInService.upplane(clockIn);
    }

    /**
     * 下机
     *
     * @param
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    // @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/deplane")
    public JsonResult deplane(@Valid @RequestBody ClockInDTO clockIn) throws Exception {
        return clockInService.deplane(clockIn);
    }

    /**
     * 结束加工任务
     *
     * @param
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    // @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/executeState")
    public JsonResult executeState(@Valid @RequestBody ClockIn clockIn) throws Exception {
        return clockInService.executeState(clockIn);
    }

    /**
     * 查询加工任务下的详细信息
     *
     *  @return
     *  @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    // @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @PostMapping("/findProductDetail")
    public JsonResult findProductDetail(@RequestBody ClockIn clockIn) throws Exception {
        return clockInService.findProductDetail(clockIn);
    }

    /**
     * 查询加工任务列表
     *
     *  @return
     *  @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    // @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/findTaskList")
    public JsonResult findTaskList(Integer state) throws Exception {
        return clockInService.findTaskList(state);
    }

    /**
     * PC端工序设计工序详情
     *
     *  @return
     *  @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @PostMapping("/getList")
    public Map getList(@RequestBody ClockInDTO clockInDTO) {
        return clockInService.getList(clockInDTO);
    }
}
